<?php
namespace Admin\Controller;

header('content-type:text/html;charset=utf8');


class CateController extends AdminController {
    public function index(){
        $res = $this->digui(0);
        $this->assign('res',$res);
        $this->display();
        //dump($res);
    }

    public function digui($id,$arr=array(),$lv=0){
        $res = M('cate')->where(array('fid'=>$id))->select();
        if($res){
            $lv++;
            foreach($res as $v){
                $v['lv'] = $lv;
                $arr[] = $v;
                $arr = $this->digui($v['id'],$arr,$lv);
                //$this-digui($v['id']);
            }
            //dump($arr);
        }
        return $arr;
    }

    public function add(){
        if(IS_POST){            
            $Cate = D("cate");
            $res = $Cate->create(I("post."));
            if($res){
                //dump($res);
               /// dump($add);
                if($Cate->add($res)){
                    $this->success('添加成功',U('index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error(D("cate")->getError());
            }
        }else{
            $Cate = D("cate");
            $id = I('get.fid');
            $title = I('get.title');
            $res = $Cate->where(array('id'=>$id))->find();
            //dump($res);
            $this->assign('id',$id);
            $this->assign('res',$res);
            $this->display();
        }
    }
}